<template>
  <MyDrawer title="资源" width="30%" :disabled="false" ref="myRef" @submit="submit">
    <el-form :model="drawerProps!.row" label-width="120" ref="formRef">
      <el-form-item label="资源名称">
        <el-input v-model="drawerProps!.row.api_name"></el-input>
      </el-form-item>
      <el-form-item label="资源路径">
        <el-input v-model="drawerProps!.row.api_path"></el-input>
      </el-form-item>
      <el-form-item label="资源类型">
        <el-input v-model="drawerProps!.row.api_type"></el-input>
      </el-form-item>
      <el-form-item label="是否进入日志">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          active-text="是"
          inactive-text="否"
          v-model="(drawerProps as any).row.is_logger"
        ></el-switch>
      </el-form-item>
      <el-form-item label="是否全局共用">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          active-text="是"
          inactive-text="否"
          v-model="(drawerProps as any).row.is_all"
        ></el-switch>
      </el-form-item>
    </el-form>
  </MyDrawer>
</template>

<script setup lang="ts">
import MyDrawer from "@/components/MyDrawer/index.vue";
import { ElMessage, FormInstance } from "element-plus";
import { ref } from "vue";

const myRef = ref<InstanceType<typeof MyDrawer> | null>();

interface DrawerProps {
  row: any;
  api?: (params: any) => Promise<any>;
  getTable: () => Promise<any>;
}

const drawerProps = ref<DrawerProps>();

const acceptParams = (params: any) => {
  myRef.value?.acceptParams();
  drawerProps.value = params;
};

const formRef = ref<FormInstance>();

const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value?.api!(drawerProps.value.row);
      ElMessage.success("成功");
      drawerProps.value?.getTable();
      myRef.value?.close();
    } catch (error) {}
  });
};

defineExpose({ acceptParams });
</script>

<style></style>
